package com.shanshan;

import org.springframework.util.StringUtils;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.Scanner;
import java.util.concurrent.Callable;

/**
 * @author hpl
 * @since 2024/5/23
 */
@Command(name = "generator", version = "generator 1.0", mixinStandardHelpOptions = true)
public class Generator implements Callable<Integer> {

    @Option(names = {"-U", "--url"}, description = "DataBase Url 数据库连接地址", echo = true)
    String url;
    @Option(names = {"-u", "--user"}, description = "DataBase User 数据库用户名", echo = true)
    String user;
    @Option(names = {"-p", "--password"}, description = "DataBase Password 数据库密码", echo = true)
    String password;
    @Option(names = {"-a", "--author"}, description = "Author 作者", echo = true)
    String author;
    @Option(names = {"-P", "--package"}, description = "Package Name 包名", echo = true)
    String packageName;
    @Option(names = {"-m", "--module"}, description = "Module Name Chinese 中文模块名称", echo = true)
    String moduleName;
    @Option(names = {"-t", "--table"}, description = "Table Name Chinese 表名", echo = true)
    String tableName;

    @Override
    public Integer call() {
        Properties config = getConfig();
        if (url == null || url.isEmpty()) {
            url = config.getProperty("url");
            if (!StringUtils.hasText(url)) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter url(数据库连接地址): ");
                url = scanner.nextLine();
            }
        }
        if (user == null || user.isEmpty()) {
            user = config.getProperty("user");
            if (!StringUtils.hasText(user)) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter user(数据库用户名): ");
                user = scanner.nextLine();
            }
        }
        if (password == null || password.isEmpty()) {
            password = config.getProperty("password");
            if (!StringUtils.hasText(password)) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter password(数据库密码): ");
                password = scanner.nextLine();
            }
        }
        if (author == null || author.isEmpty()) {
            author = config.getProperty("author");
            if (!StringUtils.hasText(author)) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter author(作者): ");
                author = scanner.nextLine();
            }
        }
        if (packageName == null || packageName.isEmpty()) {
            packageName = config.getProperty("packageName");
            if (!StringUtils.hasText(packageName)) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter packageName(包名): ");
                packageName = scanner.nextLine();
            }
        }
        if (moduleName == null || moduleName.isEmpty()) {
            moduleName = config.getProperty("moduleName");
            if (!StringUtils.hasText(moduleName)) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter moduleName(中文模块名称): ");
                moduleName = scanner.nextLine();
            }
        }
        if (tableName == null || tableName.isEmpty()) {
            tableName = config.getProperty("tableName");
            if (!StringUtils.hasText(tableName)) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("Enter tableName(表名): ");
                tableName = scanner.nextLine();
            }
        }
        DataModel dataModel = new DataModel(url, user, password, author, packageName, moduleName, tableName);
        CodeGet.generator(dataModel);
        return 0;
    }

    public static void main(String[] args) {
//        new CommandLine(new Generator()).execute("-U","-u","-p","-a","-P","-m","-t");
        int execute = new CommandLine(new Generator()).execute(args);
        System.exit(execute);
    }

    public static Properties getConfig() {
        Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream("config.properties");
             InputStreamReader reader = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
            properties.load(reader);
        } catch (IOException e) {
            System.err.println("Failed to load config file: " + e.getMessage());
        }
        return properties;
    }
}